//
//  LabelExtension.swift
//  YiZhangGui
//
//  Created by Mac on 2018/1/20.
//  Copyright © 2018年 Mac. All rights reserved.
//

import UIKit

struct RunTimeLabelKey {
    ///是否拥有复制功能
    static let isCopyable = UnsafeRawPointer.init(bitPattern: "isCopyable".hashValue)
}
//MARK: -- 初始化 --
extension UILabel{
    
    /// 初始化
    ///
    /// - Parameters:
    ///   - title: 标题
    ///   - color: 颜色
    ///   - fondSize: 大小
    ///   - TextAlignment: 位置
    convenience init(title: String, color: UIColor, fondSize: CGFloat, TextAlignment: NSTextAlignment = .left, weight: UIFont.Weight = .regular, numberOfLines: Int = 1) {
        self.init()
        self.text = title
        self.textColor = color
        self.font = UIFont.cn_fontSize(ofSize: fondSize, weight: weight)
        self.textAlignment = TextAlignment
        self.numberOfLines = numberOfLines
        
    }
    /***
     * 在Label上的文字添加另一种颜色; 可选位置和长度, 默认是尾部所有的文字.
     */
    static func createMoreColorHead_CN(text: String, textColor: UIColor, font: UIFont, backgroundColor: String = "ffffff", changeColor: UIColor, startLocation: Int, lengthOfMoreColor: Int = 0) -> UILabel {
        let label = UILabel()
        label.textColor = textColor
        label.font = font
        label.backgroundColor = UIColor(hexString: backgroundColor)
        
        var length = 0
        if lengthOfMoreColor == 0 {
            length = text.count - startLocation
        } else {
            length = text.count
        }
        let attributeStr = NSMutableAttributedString(string: text)
        attributeStr.addAttribute(NSAttributedString.Key.foregroundColor, value: changeColor, range: NSRange(location: startLocation, length: length))
        
        label.attributedText = attributeStr
        return label
    }
    
}

//MARK: -- 事件扩展 --
extension UILabel {
    
    var isCopyable: Bool? {
        set {
            objc_setAssociatedObject(self, RunTimeLabelKey.isCopyable!, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
            //添加长按手势
            self.isUserInteractionEnabled = true
            let LongPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressCopyEvent))
            self.addGestureRecognizer(LongPress)
            
        }
        get {
            return  objc_getAssociatedObject(self, RunTimeLabelKey.isCopyable!) as? Bool
        }
    }
    
    
    
    @objc func longPressCopyEvent(){
        // 让其成为响应者
        becomeFirstResponder()
        
        // 拿出菜单控制器单例
        let menu = UIMenuController.shared
        // 创建一个复制的item
        let copy = UIMenuItem(title: "复制", action: #selector(copyText))
        // 将复制的item交给菜单控制器（菜单控制器其实可以接受多个操作）
        menu.menuItems = [copy]
        // 设置菜单控制器的点击区域为这个控件的bounds
        menu.setTargetRect(bounds, in: self)
        // 显示菜单控制器，默认是不可见状态
        menu.setMenuVisible(true, animated: true)
        
    }
    
    @objc func copyText() {
        //  因为有时候 label 中设置的是attributedText
        //  而 UIPasteboard 的string只能接受 NSString 类型
        //  所以要做相应的判断
        
        if ((self.text) != nil) {
            UIPasteboard.general.string = self.text
        }else{
            UIPasteboard.general.string = self.attributedText?.string
        }
    }
    override open func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        
        if action == #selector(copyText) {
            return true
        } else {
            return false
        }
    }
    
    /// 拥有成为响应者的能力
    open override var canBecomeFirstResponder: Bool{
        return true
    }
}

//extension UILabel {
//    open override var isUserInteractionEnabled: Bool {
//        get {
//            return true
//        }
//        set {
//            // 忽略设置操作，始终将 isUserInteractionEnabled 设置为 true
//        }
//    }
//}
